package cn.mumzone.pos.ui.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.model.MemberEntity;
import cn.mumzone.pos.model.ServiceEntity;

/**
 * Created by qiuzhenhuan on 2019/6/14.
 */

public class ADA_ServiceList extends RecyclerView.Adapter<ADA_ServiceList.ViewHolder> {

    private Context mContext;

    private List<ServiceEntity> mData;

    private MemberEntity memberEntity;

    public ADA_ServiceList(Context context) {
        this.mContext = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_service_list, null);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        ServiceEntity serviceEntity = mData.get(position);

        holder.tvServiceName.setText(StringUtils.getString(serviceEntity.getServiceName()));
        holder.tvServiceTime.setText(String.format("时长：%s分钟", StringUtils.getString(serviceEntity.getServiceTime())));
        holder.tvCardTag.setVisibility(!TextUtils.isEmpty(serviceEntity.getCard()) &&
                serviceEntity.getCard().equals("1") ? View.VISIBLE : View.GONE);
        holder.tvSelectCount.setText(StringUtils.getString(serviceEntity.getSelectCount()));
        holder.tvSelectCount.setVisibility(serviceEntity.getSelectCount() == 0 ? View.GONE : View.VISIBLE);

        if (memberEntity != null && MemberEntity.DISCOUNT_TYPE_MEMBER_PRICE.equals(StringUtils.get(memberEntity.getDiscountType()))
                && !StringUtils.isEmpty(serviceEntity.getMemberPrice())) {
            holder.tvOriginPrice.setVisibility(View.VISIBLE);
            holder.tvOriginPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
            holder.tvOriginPrice.setText(StringUtils.getFormattedMoneyWithUnit(serviceEntity.getPrice()));
            holder.tvServicePrice.setText(StringUtils.getFormattedMoneyWithUnit(serviceEntity.getMemberPrice()));
        } else {
            holder.tvOriginPrice.setVisibility(View.GONE);
            holder.tvServicePrice.setText(StringUtils.getFormattedMoneyWithUnit(serviceEntity.getPrice()));
        }

    }

    public void setData(List<ServiceEntity> mData) {
        this.mData = mData;
    }

    @Override
    public int getItemCount() {
        return ListUtil.isListEmpty(mData) ? 0 : mData.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_service_name)
        TextView tvServiceName;
        @BindView(R.id.tv_card_tag)
        TextView tvCardTag;
        @BindView(R.id.tv_service_time)
        TextView tvServiceTime;
        @BindView(R.id.tv_origin_price)
        TextView tvOriginPrice;
        @BindView(R.id.tv_service_price)
        TextView tvServicePrice;
        @BindView(R.id.tv_select_count)
        TextView tvSelectCount;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setBindMember(MemberEntity bindMember) {
        memberEntity = bindMember;
    }

    public List<ServiceEntity> getData() {
        return mData;
    }


    public void setItemSelectCount(String id, int count) {
        for (ServiceEntity entity : mData) {
            if (entity.getServiceId().equals(id)) {
                entity.setSelectCount(count);
            }
        }
    }
}
